from wormbane.direction import direction_to_planar_vector
from wormbane.Feature import Door, Doorway, Floor, Wall
import curses


class CursesUi(object):
    def __init__(self, stdscr):
        self.stdscr = stdscr
        self.command_shortcuts = {ord('7'): 'northwest',
                                  ord('8'): 'north',
                                  ord('9'): 'northeast',
                                  ord('4'): 'west',
                                  ord('5'): 'rest',
                                  ord('6'): 'east',
                                  ord('1'): 'southwest',
                                  ord('2'): 'south',
                                  ord('3'): 'southeast',
                                  ord('<'): 'up',
                                  ord('>'): 'down',
                                  curses.KEY_UP: 'north',
                                  curses.KEY_LEFT: 'west',
                                  curses.KEY_RIGHT: 'east',
                                  curses.KEY_DOWN: 'south',
                                  ord('c'): 'close',
                                  ord('o'): 'open',
                                  ord('Q'): 'quit',
                                  ord('s'): 'search',
                                  ord('S'): 'save'}
        self.wall_chars = {'northwest': curses.ACS_ULCORNER,
                           'north': curses.ACS_HLINE,
                           'northeast': curses.ACS_URCORNER,
                           'west': curses.ACS_VLINE,
                           None: ord(' '),
                           'east': curses.ACS_VLINE,
                           'southwest': curses.ACS_LLCORNER,
                           'south': curses.ACS_HLINE,
                           'southeast': curses.ACS_LRCORNER}
        self.door_chars = {'+': '+',
                           '|': curses.ACS_VLINE,
                           '-': curses.ACS_HLINE}
        self.colors = {'black': curses.color_pair(curses.COLOR_BLACK),
                       'blue': curses.color_pair(curses.COLOR_BLUE),
                       'cyan': curses.color_pair(curses.COLOR_CYAN),
                       'green': curses.color_pair(curses.COLOR_GREEN),
                       'magenta': curses.color_pair(curses.COLOR_MAGENTA),
                       'red': curses.color_pair(curses.COLOR_RED),
                       'yellow': curses.color_pair(curses.COLOR_YELLOW),
                       'white': curses.color_pair(curses.COLOR_WHITE)}
        self.vitality_colors = {'good': 'green',
                                'fair': 'yellow',
                                'poor': 'red'}

    def tile_to_char(self, tile):
        if tile.top_item:
            return ord(tile.top_item.char), tile.top_item.color
        elif tile.feature:
            if type(tile.feature) is Wall:
                return self.wall_chars[tile.feature.dir], tile.feature.color
            elif type(tile.feature) is Door:
                return self.door_chars[tile.feature.char], tile.feature.color
            elif type(tile.feature) in (Floor, Doorway):
                return curses.ACS_BULLET, tile.feature.color
            else:
                return ord(tile.feature.char), tile.feature.color
        else:
            return ord(' '), 'black'

    def update_screen(self, game):
        stdscr = self.stdscr
        stdscr.addstr(0, 0, ' '.join(game.messages).ljust(79)[:79])
        width, height = game.current_level.size
        tiles = game.current_level.tiles
        light = game.current_level.light
        sight = game.current_level.sight
        colors = self.colors
        tile_to_char = self.tile_to_char
        for column in tiles:
            for tile in column:
                if tile.pos in sight and (tile.lit or tile.pos in light):
                    char, color = tile_to_char(tile)
                    tile.memory = char
                    if tile.monster is not None:
                        char, color = tile.monster.char, tile.monster.color
                else:
                    char = tile.memory
                    color = 'blue'
                x, y = tile.pos
                stdscr.addch(height - y, x, char,
                             colors[color])
        stdscr.addstr(height + 1, 0, 'Wormhole %d  ' % game.current_level.num)
        vitality_percentage = game.hero.vitality_percentage
        if vitality_percentage < 100:
            vitality_color = self.vitality_colors[game.hero.vitality_shape]
        else:
            vitality_color = 'black'
        stdscr.addstr('Vitality %d%%  ' % vitality_percentage,
                      self.colors[vitality_color])
        if game.hero.hungry:
            stdscr.addstr('Hungry  ')
        x, y = game.hero.pos
        stdscr.move(height - y, x)

    def read_command(self):
        while True:
            ch = self.stdscr.getch()
            if ch in self.command_shortcuts:
                return self.command_shortcuts[ch]
